<template>
  <div class="app-container">
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.$index + 1 + (pagination.pageNumber - 1) * pagination.pageSize }}
        </template>
      </el-table-column>
      <el-table-column label="姓名">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="手机号码">
        <template slot-scope="scope">
          {{ scope.row.phone }}
        </template>
      </el-table-column>
      <el-table-column label="申请简要内容">
        <template slot-scope="scope">
          {{ scope.row.resource | ellipsis }}
        </template>
      </el-table-column>
      <el-table-column label="是否已回复" width="180">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isDispose"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="已回复"
            inactive-text="未回复"
            @change="useChane(scope.row.isDispose, scope.row._id)"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="日期" width="230">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.date }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="success" size="mini" @click="handleView(scope.row)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        background
        :current-page.sync="pagination.pageNumber"
        :page-size="pagination.pageSize"
        layout="total, prev, pager, next"
        :total="pagination.total"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 弹框 -->
    <el-dialog title="查看详情" :visible.sync="dialogFormVisible">
      <el-row :gutter="20" class="elRow">
        <el-col :span="6"> 姓名： </el-col>
        <el-col :span="12"> {{ dialogData.name }} </el-col>
      </el-row>
      <el-row :gutter="20" class="elRow">
        <el-col :span="6"> 手机号码： </el-col>
        <el-col :span="12"> {{ dialogData.phone }} </el-col>
      </el-row>
      <el-row :gutter="20" class="elRow">
        <el-col :span="6"> 申请内容：</el-col>
        <el-col :span="16">
          {{ dialogData.resource }}
        </el-col>
      </el-row>
      <el-row :gutter="20" class="elRow">
        <el-col :span="6"> 是否已回复： </el-col>
        <el-col :span="12" class="elRow">
          {{ dialogData.isDispose ? '已回复' : '未回复' }}
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false"> 关闭 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getApplicationList, changeIsDispose } from '@/api/application'
export default {
  name: 'Application',
  data() {
    return {
      pagination: {
        total: 0,
        pageSize: 10,
        pageNumber: 1
      },
      options: [
        {
          value: true,
          label: '已回复'
        },
        {
          value: false,
          label: '未回复'
        }
      ],
      list: null,
      listLoading: false,
      dialogFormVisible: false,
      dialogData: {
        name: '',
        phone: '',
        resource: '',
        isDispose: null,
        date: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 选择页码
    handleCurrentChange(val) {
      this.pagination.pageNumber = val
      this.fetchData()
    },
    // 拉表格数据
    async fetchData() {
      var { pageSize, pageNumber } = this.pagination
      this.listLoading = true
      try {
        const response = await getApplicationList({ pageSize, pageNumber })
        this.list = response.data
        this.listLoading = false
        this.pagination = response.pagination
      } catch (error) {
        console.log('error: ', error)
        this.listLoading = false
      }
    },
    handleView(row) {
      console.log('row: ', row)
      this.dialogData = row
      this.dialogFormVisible = true
    },
    async useChane(isDispose, id) {
      console.log('id: ', id)
      console.log('isDispose: ', isDispose)
      try {
        await changeIsDispose({
          id,
          isDispose
        })
        this.fetchData()
      } catch (error) {
        console.log('error: ', error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.elRow {
  margin-bottom: 10px;
}
.block {
  margin-top: 20px;
  text-align: center;
}
</style>
